package com.veepeak.server.api.web;

import com.alibaba.fastjson.JSONObject;
import com.veepeak.server.common.JSONResult;
import com.veepeak.server.domain.DtcCommand;
import com.veepeak.server.domain.ModelCommand;
import com.veepeak.server.dto.PageModel;
import com.veepeak.server.service.DtcCommandService;
import com.veepeak.server.service.ModelCommandService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping(value = "/web/command")
@Api(description = "command接口")
public class CommandWebApi {
    @Autowired
    private DtcCommandService dtcCommandService;
    @Autowired
    private ModelCommandService modelCommandService;

    @PostMapping(value = "/dtc")
    public JSONObject dtcList(HttpServletRequest request, PageModel pageModel){
        //request.getParameterMap().keySet().forEach(key -> System.out.println(key+"  -->  "+request.getParameter(key)));
        String searchValue = request.getParameter("search[value]");

        Page<DtcCommand> dtcCommandPage = dtcCommandService.datatables(pageModel.getPage(),pageModel.getLength(),searchValue);

        return JSONResult.toSuccessJson(new PageModel(pageModel.getDraw(),dtcCommandPage),"").getJSONObject("data");
    }

    @PostMapping(value = "/model")
    public JSONObject modelList( PageModel pageModel){
        Page<ModelCommand> modelCommandPage = modelCommandService.findAll(PageRequest.of(pageModel.getPage(),pageModel.getLength()));

        return JSONResult.toSuccessJson(new PageModel(pageModel.getDraw(),modelCommandPage),"").getJSONObject("data");
    }


}
